package com.link.base.base.activity.controller;

import com.link.base.user.model.CoreUser;
import com.link.base.base.activity.model.Activitys;
import com.link.base.base.activity.service.ActivityService;
import com.link.core.cllog.LCLogger;
import com.link.core.basic.annotations.JsonParam;
import com.link.core.basic.controller.BasicController;
import com.link.core.basic.interceptor.SecurityInterceptor;
import com.link.core.basic.service.BasicService;
import com.link.core.util.LovUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * Copyright  hande Company. All Rights Reserved.
 *
 * @author unknwon
 * @version 1.0
 * @date: 2018/11/8 9:19
 */

@Controller
@RequestMapping("/link/activity")
public class ActivityController extends BasicController<Activitys> {
    @Resource
    private ActivityService activityService;

    @Override
    public BasicService<Activitys> getBasicService() throws Exception {
        return activityService;
    }

    /**
     * 取默认值
     *
     * @param entity
     * @param session
     * @param request
     * @param response
     * @return
     */
    @Override
    @RequestMapping(value = "/preDefaultValue", method = RequestMethod.POST, produces = "application/json")
    @ResponseBody
    public Map<String, Object> preDefaultValue(@JsonParam Activitys entity, HttpSession session, HttpServletRequest request,
                                               HttpServletResponse response) {
        Map<String, Object> result = new HashMap<String, Object>(4);
        try {
            Activitys newEntity = new Activitys();
            //回访
            String typeLovName = LovUtil.getLovName("TUDO_CD", "RETURN_VISIT_CD");
            newEntity.setType(typeLovName);
            //中
            String importantLovName = LovUtil.getLovName("ACTIVITY_IMPORTANT", "ACTIVITY_IMPORTANT_M");
            newEntity.setImportant(importantLovName);
            //未开始
            String statusLovName = LovUtil.getLovName("EVT_STAT_CD", "EVT_STAT_NO_START");
            newEntity.setStatus(statusLovName);
            CoreUser user = SecurityInterceptor.tempUser.get();
            if (user != null) {
                Long orgId = user.getOrgId();
                newEntity.setOrgId(orgId);
                String orgName = user.getOrgName();
                newEntity.setOrgName(orgName);
                Long postnId = user.getPostnId();
                newEntity.setPostnId(postnId);
                String postnName = user.getPostnName();
                newEntity.setPostnName(postnName);

                SimpleDateFormat dfFormat = new SimpleDateFormat("yyyy-MM-dd-yyyy HH:mm:ss");
                Date myDate = new Date();
                String sysdate = dfFormat.format(myDate);
                newEntity.setCreated(sysdate);
                newEntity.setPlanStartDt(sysdate);

            }
            result.put("success", true);
            result.put("result", newEntity);
        } catch (Exception e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("result", e.getMessage());
        }
        return result;
    }

}
